package com.tianshuo.beta.sso.ticket.registry;

import com.tianshuo.beta.sso.mapper.ServiceTicketMapper;
import com.tianshuo.beta.sso.mapper.TicketMapper;
import com.tianshuo.beta.sso.model.ServiceTicketDO;
import com.tianshuo.beta.sso.model.TicketDO;
import com.tianshuo.beta.sso.ticket.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class JdbcRegistry implements TicketRegistry {

    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private ServiceTicketMapper serviceTicketMapper;

    @Override
    public void addTicket(Ticket ticket) {
        Date now = new Date();
        if (ticket instanceof LoginTicket) {
            LoginTicket loginTicket = (LoginTicket) ticket;
            TicketDO ticketDO = new TicketDO();
            ticketDO.setTicketId(loginTicket.getId());
            ticketDO.setCreatedTime(now);
            ticketDO.setUpdatedTime(now);
            ticketMapper.insert(ticketDO);
        } else if (ticket instanceof ServiceTicket) {
            ServiceTicket serviceTicket = (ServiceTicket) ticket;
            ServiceTicketDO serviceTicketDO = new ServiceTicketDO();
            serviceTicketDO.setTicketId(serviceTicket.getLoginTicket().getId());
            serviceTicketDO.setServiceTicketId(serviceTicket.getId());
            serviceTicketDO.setService(serviceTicket.getService());
            serviceTicketDO.setCreatedTime(now);
            serviceTicketDO.setUpdatedTime(now);
            serviceTicketMapper.insert(serviceTicketDO);
        }
    }

    @Override
    public Ticket getTicket(String ticketId) {
        // 查询登录票据
        TicketDO ticketDO = ticketMapper.selectByTicketId(ticketId);
        if (ticketDO != null) {
            LoginTicketImpl loginTicket = new LoginTicketImpl();
            loginTicket.setId(ticketDO.getTicketId());
            return loginTicket;
        }

        // 查询服务票据
        ServiceTicketDO serviceTicketDO = serviceTicketMapper.selectByServiceTicketId(ticketId);
        if (serviceTicketDO != null) {
            ServiceTicketImpl serviceTicket = new ServiceTicketImpl();
            serviceTicket.setService(serviceTicketDO.getService());
            // 设置关联的登录票据
            LoginTicket loginTicket = (LoginTicket) getTicket(serviceTicketDO.getTicketId());
            serviceTicket.setLoginTicket(loginTicket);
            return serviceTicket;
        }
        return null;
    }

    @Override
    public boolean deleteTicket(String ticketId) {
        // 先尝试删除登录票据
        int rows = ticketMapper.deleteByTicketId(ticketId);
        if (rows > 0) {
            // 级联删除关联的服务票据
            serviceTicketMapper.deleteByTicketId(ticketId);
            return true;
        }
        // 删除服务票据
        return serviceTicketMapper.deleteByServiceTicketId(ticketId) > 0;
    }

    @Override
    public List<Ticket> getAllTicket() {
        List<TicketDO> ticketDOList = ticketMapper.selectAll();
        List<Ticket> tickets = new ArrayList<>(ticketDOList.size());
        
        for (TicketDO ticketDO : ticketDOList) {
            LoginTicketImpl loginTicket = new LoginTicketImpl();
            loginTicket.setId(ticketDO.getTicketId());
            tickets.add(loginTicket);
        }
        
        return tickets;
    }

    public boolean updateTicket(Ticket ticket) {
        Date now = new Date();
        if (ticket instanceof LoginTicket) {
            TicketDO ticketDO = new TicketDO();
            ticketDO.setTicketId(ticket.getId());
            ticketDO.setUpdatedTime(now);
            return ticketMapper.updateByTicketId(ticketDO) > 0;
        } else if (ticket instanceof ServiceTicket) {
            ServiceTicketDO serviceTicketDO = new ServiceTicketDO();
            serviceTicketDO.setServiceTicketId(ticket.getId());
            serviceTicketDO.setUpdatedTime(now);
            return serviceTicketMapper.updateByServiceTicketId(serviceTicketDO) > 0;
        }
        return false;
    }
}
